//
//  DYNavigationCOntroller.m
//  DYLYJ
//
//  Created by JM on 15/10/28.
//  Copyright © 2015年 JM. All rights reserved.
//

#import "DYNavigationCOntroller.h"
#import "UIColor+DY.h"
#import "UIBarButtonItem+DY.h"
#import <YYCategories.h>
@implementation DYNavigationCOntroller
#pragma mark 一个类只会调用一次
+ (void)initialize
{
    // 1.取出设置主题的对象
    UINavigationBar *navBar = [UINavigationBar appearance];
//    UIBarButtonItem *barItem = [UIBarButtonItem appearance];
    //导航栏背景
    [navBar setBackgroundImage:[UIImage imageNamed:@"naviBar"] forBarMetrics:UIBarMetricsDefault];
    kDYBarWidth = [UIScreen mainScreen].bounds.size.width;
    

     
//    //修改导航条上的UIBarButtonItem
//    UIBarButtonItem *appearance = [UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil];
//    
//    //设置导航栏的字体包括backBarButton和leftBarButton，rightBarButton的字体
//    NSDictionary *textAttributes = @{UITextAttributeFont: [UIFont systemFontOfSize:18],
//                                     UITextAttributeTextColor: [UIColor blueColor],
//                                     UITextAttributeTextShadowColor: [UIColor whiteColor],
//                                     UITextAttributeTextShadowOffset: [NSValue valueWithCGSize:CGSizeMake(1, 1)]};
//    
//    [appearance setTitleTextAttributes:textAttributes forState:1];//forState为0时为下正常状态，为1时为点击状态。
//    
//    
////    //修改leftBarButton，rightBarButton背景效果
////    [appearance setBackgroundImage:[UIImage imageNamed:@"backBtn.png"]
////                          forState:UIControlStateNormal
////                             style:UIBarButtonItemStyleBordered
////                        barMetrics:UIBarMetricsDefault];
//
//    
//    //backBarButton需要单独设置背景效果。只能在ios6.0以后才能用
//    [appearance setBackButtonBackgroundImage:[UIImage imageNamed:@"backBtn.png"]
//                                    forState:UIControlStateNormal
//                                  barMetrics:UIBarMetricsDefault];
//    
//    [appearance setBackButtonTitlePositionAdjustment:UIOffsetMake( -100, -100)
//                                       forBarMetrics:UIBarMetricsDefault];
//    [appearance setBackButtonBackgroundVerticalPositionAdjustment:-12 forBarMetrics:UIBarMetricsDefault];


    
    
//        // 设置导航栏的渐变色为白色（iOS7中返回箭头的颜色变为这个颜色：白色）
//        navBar.tintColor = [UIColor whiteColor];
    
//    // 3.设置导航栏标题颜色为白色
//    [navBar setTitleTextAttributes:@{
//                                     NSForegroundColorAttributeName : [UIColor whiteColor]
//                                     }];
//
//    // 4.设置导航栏按钮文字颜色为白色
//    [barItem setTitleTextAttributes:@{
//                                      NSForegroundColorAttributeName : [UIColor whiteColor],
//                                      NSFontAttributeName : [UIFont systemFontOfSize:13]
//                                      } forState:UIControlStateNormal];
    
    //获取应用程序中所有的导航条
//    UINavigationBar *bar = [UINavigationBar appearance];
//    [bar setBarTintColor:[UIColor colorWithRed:20/255.0 green:155/255.0 blue:240/255.0 alpha:1.0]];
//    NSDictionary *dict = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:18 weight:bold]};
//    [bar setTitleTextAttributes:dict];
}

//#pragma mark 控制状态栏的样式
///*
// 状态栏的管理：
// 1> iOS7之前：UIApplication
// 2> iOS7开始：交给对应的控制器去管理
// */
//- (UIStatusBarStyle)preferredStatusBarStyle
//{
//    // 白色样式
//    return UIStatusBarStyleLightContent;
//}
//
//- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
//{
//    if (self.viewControllers.count) {
//        viewController.hidesBottomBarWhenPushed = YES;
//    }
//    [super pushViewController:viewController animated:animated];
//}

//- (instancetype)initWithCoder:(NSCoder *)coder
//{
//    self = [super initWithCoder:coder];
//    if (self) {
//        <#statements#>
//    }
//    return self;
//}

- (void)changeNaviBarPushedViewController:(UIViewController *)vc {
    
    vc.view.backgroundColor =  UIColorFromHexStr(@"#F6F1E8");
    vc.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navibar_title"]];
    [self.navigationBar setTitleVerticalPositionAdjustment:-(kDYBarHeight-self.topViewController.navigationItem.titleView.frame.size.height)/2.0 forBarMetrics:UIBarMetricsDefault];
    
    if (!vc.navigationItem.leftBarButtonItem && (self.viewControllers.firstObject != nil) && (self.viewControllers.firstObject != vc)) {
        vc.navigationItem.leftBarButtonItem = [UIBarButtonItem backBarItemWithTarget:self action:@selector(clickBackButton:)];
    }
    
}


- (void) pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [self changeNaviBarPushedViewController:viewController];
    [super pushViewController:viewController animated:animated];
}

- (void)viewWillAppear:(BOOL)animated {
   //push 不会调用此函数。
//    NSLog(@"%s",__FUNCTION__);
    [self changeNaviBarPushedViewController:self.topViewController];
    [super viewWillAppear:animated];
}



- (void)clickBackButton:(id)sender {
    [self popViewControllerAnimated:YES];
}
@end

@implementation UINavigationBar (changeHeigh)
- (CGSize)sizeThatFits:(CGSize)size {
    NSString *model = [UIDevice currentDevice].machineModel;
    if ([model hasPrefix:@"iPhone4"] || [model containsString:@"iPad"]) {
        return CGSizeMake(kDYBarWidth,44);
    }
    NSInteger width = kDYBarWidth;
    CGSize newSize = CGSizeMake(width,kDYBarHeight);
    return newSize;
}

@end